/*java jdk 1.7+
# -*- coding: utf-8 -
'''
Created on 2020年05月10日
@author: zxyong 13738196011
'''*/

package com.zxy.db4;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;
import com.zxy.model.T_PROC_NAME;

public class Db_Common4 implements com.zxy.common.Db_Common_InterFace
{
	public StringBuilder		sqlException	= new StringBuilder();
	public ResultSet			rs				= null;
	public PooledConnection4	conn_a			= null;
	public int lastrowid =  -1;
	public int rowsCount =  0;
	public int pageNumber = 0;
	public int pageSize = 0;

	// 开始事务
	public PooledConnection4 BeginTransaction() throws SQLException
	{
		conn_a = DBManager4.getConnection();
		conn_a.BeginTranSaction();
		return conn_a;
	}

	// 执行查询sql语句
	public ResultSet Common_Sql(StringBuilder strSql)
	{
		try
		{
			sqlException.delete(0,sqlException.length());
			conn_a = DBManager4.getConnection();
			rs = conn_a.executeQuery(strSql);
		}
		catch(SQLException e)
		{
			sqlException.delete(0,sqlException.length());
			sqlException.append("数据库操作出错请查看程序错误日志文件:" + e.getMessage());
			Com_Fun.debug_in(e,this.toString(),strSql.toString());
		}
		finally
		{
			conn_a.returnConnection();
		}
		return rs;
	}

	/*
	 * ProName 存储过程名 Parameters输入参数 ParamTypes参数类型String Int float Date
	 * ParamOutName输出参数名 ParamOutType输出参数类型
	 */
	public ResultSet Common_Sql_Proc(String ProName, String[] Parameters, String[] ParamTypes, String[] ParamOutName, String[] ParamOutType, T_PROC_NAME trn)
	{
		try
		{
			sqlException.delete(0,sqlException.length());
			conn_a = DBManager4.getConnection();
			if(trn.INF_TYPE.equals("1"))
				rs = conn_a.executeProcedure(ProName,Parameters,ParamTypes,ParamOutName,ParamOutType);
			else{
				rs = conn_a.ParamExecuteQuery(ProName,Parameters,ParamTypes,ParamOutName,ParamOutType,new StringBuilder(trn.INF_EN_SQL));
				this.lastrowid = conn_a.lastrowid;
				this.rowsCount = conn_a.rowsCount;
			}
		}
		catch(Exception e)
		{
			sqlException.delete(0,sqlException.length());
			sqlException.append("数据库操作出错请查看程序错误日志文件:" + ProName + " "
					+ e.getMessage());
			Com_Fun.debug_in(e,this.toString(),"数据库操作出错请查看程序错误日志文件:" + ProName);
		}
		finally
		{
			conn_a.returnConnection();
		}
		return rs;
	}

	/*
	 * ProName 存储过程名 Parameters输入参数 ParamTypes参数类型String Int float Date
	 * ParamOutName输出参数名 ParamOutType输出参数类型
	 */
	public ResultSet Common_Sql_Proc(PooledConnection4 conn, String ProName, String[] Parameters, String[] ParamTypes, String[] ParamOutName, String[] ParamOutType) throws SQLException
	{
		try
		{
			sqlException.delete(0,sqlException.length());
			rs = conn.executeProcedure(ProName,Parameters,ParamTypes,ParamOutName,ParamOutType);
		}
		catch(Exception e)
		{
			sqlException.delete(0,sqlException.length());
			sqlException.append("数据库操作出错请查看程序错误日志文件:" + ProName + " "
					+ e.getMessage());
			Com_Fun.debug_in(e,this.toString(),"数据库操作出错请查看程序错误日志文件:" + ProName);
			conn.RollBackTranSaction();
		}
		return rs;
	}

	// / <summary>
	// / 执行查询sql语句
	// / </summary>
	public ResultSet Common_Sql(PooledConnection4 conn, StringBuilder strSql) throws SQLException
	{
		try
		{
			sqlException.delete(0,sqlException.length());
			rs = conn.executeQuery(strSql);
		}
		catch(SQLException e)
		{
			sqlException.delete(0,sqlException.length());
			sqlException.append("数据库操作出错请查看程序错误日志文件:" + e.getMessage());
			Com_Fun.debug_in(e,this.toString(),strSql.toString());
			conn.RollBackTranSaction();
		}
		return rs;
	}

	public int CommonExec_SqlNoCon(PooledConnection4 conn, StringBuilder strSql)
	{
		int iResult = -1;
		try
		{
			sqlException.delete(0,sqlException.length());
			iResult = conn.executeUpdate(strSql);
		}
		catch(SQLException e)
		{
			sqlException.delete(0,sqlException.length());
			sqlException.append("数据库操作出错请查看程序错误日志文件:" + e.getMessage());
			iResult = -1;
			Com_Fun.debug_in(e,this.toString(),strSql.toString());
			try
			{
				conn.RollBackTranSaction();
			}
			catch(SQLException e1)
			{
			}
		}
		return iResult;
	}

	// / <summary>
	// / 执行sql语句 增加、修改、删除
	// / </summary>
	// / <param name="strSql"></param>
	public int CommonExec_Sql(StringBuilder strSql)
	{
		int iResult = -1;
		try
		{
			sqlException.delete(0,sqlException.length());
			conn_a = DBManager4.getConnection();
			iResult = conn_a.executeUpdate(strSql);
		}
		catch(SQLException e)
		{
			sqlException.delete(0,sqlException.length());
			sqlException.append("数据库操作出错请查看程序错误日志文件:" + e.getMessage());
			Com_Fun.debug_in(e,this.toString(),strSql.toString());
			iResult = -1;
		}
		finally
		{
			conn_a.returnConnection();
		}
		return iResult;
	}

	// <summary>
	// 执行sql语句 增加、修改、删除
	// </summary>
	// <param name="strSql"></param>
	public int CommonExec_Sql(PooledConnection4 conn, StringBuilder strSql) throws SQLException
	{
		int iResult = -1;
		try
		{
			sqlException.delete(0,sqlException.length());
			iResult = conn.executeUpdate(strSql);
		}
		catch(SQLException e)
		{
			sqlException.delete(0,sqlException.length());
			sqlException.append("数据库操作出错请查看程序错误日志文件:" + e.getMessage());
			iResult = -1;
			Com_Fun.debug_in(e,this.toString(),strSql.toString());
			conn.RollBackTranSaction();
		}
		return iResult;
	}

	public Object getConn_a(){
		return (Object)this.conn_a;
	}

	// 根据表名获取最大MAIN_ID
	public int GetMax_Main_Id(PooledConnection4 conn, String Table_Name) throws SQLException
	{
		int iResult = 1;
		return iResult;
	}

	/**
	 * @return sqlException
	 */
	public StringBuilder getSqlException()
	{
		return sqlException;
	}

	/**
	 * @param sqlException 要设置的 sqlException
	 */
	public void setSqlException(StringBuilder sqlException)
	{
		this.sqlException = sqlException;
	}

	/**
	 * @return rs
	 */
	public ResultSet getRs()
	{
		return rs;
	}

	/**
	 * @param rs 要设置的 rs
	 */
	public void setRs(ResultSet rs)
	{
		this.rs = rs;
	}

	/**
	 * @return lastrowid
	 */
	public int getLastrowid()
	{
		return lastrowid;
	}

	/**
	 * @param lastrowid 要设置的 lastrowid
	 */
	public void setLastrowid(int lastrowid)
	{
		this.lastrowid = lastrowid;
	}

	/**
	 * @return rowsCount
	 */
	public int getRowsCount()
	{
		return rowsCount;
	}

	/**
	 * @param rowsCount 要设置的 rowsCount
	 */
	public void setRowsCount(int rowsCount)
	{
		this.rowsCount = rowsCount;
	}

	/**
	 * @param conn_a 要设置的 conn_a
	 */
	public void setConn_a(PooledConnection4 conn_a)
	{
		this.conn_a = conn_a;
	}

	/**
	 * @return pageNumber
	 */
	public int getPageNumber()
	{
		return pageNumber;
	}

	/**
	 * @param pageNumber 要设置的 pageNumber
	 */
	public void setPageNumber(int pageNumber)
	{
		this.pageNumber = pageNumber;
	}

	/**
	 * @return pageSize
	 */
	public int getPageSize()
	{
		return pageSize;
	}

	/**
	 * @param pageSize 要设置的 pageSize
	 */
	public void setPageSize(int pageSize)
	{
		this.pageSize = pageSize;
	}
}